<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="所属商会" prop="chamber">
        <el-select v-model="queryParams.chamber" placeholder="请选择所属商会" clearable>
          <el-option
            v-for="dict in dict.type.sys_no_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="是否为党员" prop="isPartyMember" label-width="200" >
        <el-select v-model="queryParams.isPartyMember" placeholder="请选择是否为党员" clearable >
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除
        </el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-user"
          size="mini"
          :disabled="multiple"
          @click="handleDelete2"
        >联系人分组
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="username !== 'admin'"
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleDelete3"
        >设置为明星会员
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="电话" align="center" prop="phone"/>
      <el-table-column label="所属商会" align="center" prop="chamber">
        <template slot-scope="scope">
          <template v-if="scope.row.chamber">
            <span>{{ scope.row.chamber }}</span>
          </template>
          <template v-else>
            <span>暂未加入商会</span>
          </template>
        </template>
      </el-table-column>
      <!--      <el-table-column label="所属商会" align="center" prop="chamber" type="primary" />-->
      <!--        todo-->
      <!--        <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_no_type" :value="scope.row.chamber"/>
              </template>-->
      <!--        <template slot-scope="scope">
                <dict-tag :options=this.listChamberLable :value="scope.row.chamber"/>
              </template>-->
      <el-table-column label="是否为党员" align="center" prop="isPartyMember">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isPartyMember"/>
        </template>
      </el-table-column>
      <el-table-column label="是否为明星会员" align="center" prop="isStarMember" v-if="username !== 'admin'">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isStarMember"/>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" key="enabled" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
<!--      <el-table-column label="启用状态" align="center" prop="enabled">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.enabled"/>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >查看
          </el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看会员基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号" prop="account">
              <el-input v-model="form.account" placeholder="请输入账号" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="身份证号码" prop="idCard" label-width="100px">
          <el-input v-model="form.idCard" placeholder="请输入身份证号码" :disabled="true"/>
        </el-form-item>
        <el-form-item label="常住地" prop="residence" label-width="100px">
          <el-input v-model="form.residence" placeholder="请输入常住地" :disabled="true"/>
        </el-form-item>
        <el-form-item label="籍贯" prop="hometown" label-width="100px">
          <el-input v-model="form.hometown" placeholder="请输入籍贯" :disabled="true"/>
        </el-form-item>
<!--        <el-form-item label="所属商会" prop="chamber" label-width="100px">
          <el-select v-model="form.chamber" placeholder="请选择所属商会">
            <el-option
              v-for="option in this.listChamberLable"
              :key="option.value"
              :label="option.label"
              :value="option.value"
            ></el-option>
          </el-select>
        </el-form-item>-->
<!--        <el-row>
          <el-col :span="12">
            <el-form-item label="是否为党员">
              <el-radio-group v-model="form.isPartyMember" label-width="120px">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否为明星会员" label-width="120px" >
              <el-radio-group v-model="form.isStarMember">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="启用状态">
          <el-radio-group v-model="form.enabled">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.rectangle-box {
  display: inline-block;
  padding: 4px 8px;
  background: #d6e9fa;
  border: 1px solid #d6e9fa;
  border-radius: 4px;
  color: #0037ff;
}
</style>
<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from '@/api/system/info'
import request from '@/utils/request'
import userInfo from '@/views/system/user/profile/userInfo.vue'
import { listChamberLable } from '@/api/system/groups'
import { changeUserStatusInfo } from '@/api/system/user'
import Cookies from 'js-cookie'

export default {
  name: 'Info',
  dicts: ['sys_no_type', 'sys_yes_no', 'sys_normal_disable'],
  props: {
    username: {
      type: String,
      default: '',
    },
    isStarMember: {
      type: String,
      default: '',
    },
    isPartyMember: {
      type: String,
      default: '',
    }
  },
  data() {
    return {
      username : Cookies.get("username"),
      listChamberLable: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员基本信息表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        chamber: null,
        isStarMember: null,
        isPartyMember: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: '身份证号码不能为空', trigger: 'blur' }
        ],
        residence: [
          { required: true, message: '常住地不能为空', trigger: 'blur' }
        ],
        hometown: [
          { required: true, message: '籍贯不能为空', trigger: 'blur' }
        ],
        /* chamber: [
          { required: true, message: '所属商会不能为空', trigger: 'change' }
        ], */
        isPartyMember: [
          { required: true, message: '是否为党员不能为空', trigger: 'blur' }
        ],
        isStarMember: [
          { required: true, message: '是否为明星会员不能为空', trigger: 'blur' }
        ],
        enabled: [
          { required: true, message: '启用状态不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    console.log(this.username,'11111111')
    listChamberLable().then(response => {
      this.listChamberLable = response.data.rows.map(function(item) {
        return { label: item.label, value: item.value }
      })
    })
  },
  methods: {
    handleStatusChange(row) {
      let text = row.enabled === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"用户吗？').then(function() {
        return changeUserStatusInfo(row.id,row.name, row.enabled);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.enabled = row.enabled === "0" ? "1" : "0";
      });
    },
    /** 查询会员基本信息列表 */
    getList() {
      console.log('username',this.username)
      if(this.username.length>0){
        this.queryParams.name=this.username
      }
      if(this.isStarMember.length>0){
        this.queryParams.isStarMember=this.isStarMember
      }
      if(this.isPartyMember.length>0){
        this.queryParams.isPartyMember=this.isPartyMember
      }
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        account: null,
        password: null,
        idCard: null,
        residence: null,
        hometown: null,
        chamber: null,
        isPartyMember: 0,
        isStarMember: 0,
        enabled: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加会员基本信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看会员基本信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除会员基本信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    showTextInputModal() {
      return new Promise((resolve, reject) => {
        this.$prompt('请输入分组名称', '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /.+/,
          inputErrorMessage: '分组名称不能为空'
        }).then(({ value }) => {
          resolve(value) // 将输入的文本值传递给 Promise 的 resolve
        }).catch(() => {
          reject() // 如果取消或关闭弹框，则调用 Promise 的 reject
        })
      })
    },
    /** 分组操作 */
    handleDelete2(row) {
      this.$confirm('是否确认将所选联系人分为一组', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        customClass: 'custom-confirm'
      }).then(() => {
        return this.showTextInputModal()
      }).then((inputValue) => {
        // 在这里处理输入的文本值
        console.log('输入的文本值:', this.$store.state.user.name)
        const ids = row.id || this.ids
        return request({
          url: '/system/info/mergePeople/' + ids + '/' + inputValue,
          method: 'get'
        })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('操作成功')
      }).catch(() => {
        this.getList()
        // 取消操作或关闭弹框时的处理逻辑
      }).finally(() => {
        this.getList()
      })
    },
    /** 设置为明星会员 */
    handleDelete3(row) {
      this.$confirm('是否将所选联系人设置为明星会员', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        customClass: 'custom-confirm'
      }).then(() => {
        // 在这里处理输入的文本值
        const ids = row.id || this.ids
        return request({
          url: '/system/info/updateStar/' + ids,
          method: 'put'
        })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('操作成功')
      }).catch(() => {
        this.getList()
        // 取消操作或关闭弹框时的处理逻辑
      }).finally(() => {
        this.getList()
      })

      /* this.$modal.confirm('是否确认将所选联系人分为一组').then(function() {
        this.$modal.confirm('是否确认将所选联系人分为一组2').then(function() {
          const ids = row.id || this.ids;
          return request({
            url: '/system/info/mergePeople' + ids,
            method: 'post'
          })
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("分组成功");
        }).catch(() => {});
      }) */
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
